<?php

namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;
use App\Helpers\TengOcrTrait;
use App\Http\Controllers\Shop\ApplymentsController;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use OSS\OssClient;
use OSS\Core\OssException;
trait OssController
{
    use TengOcrTrait;
    public function commonUpload($request,$path='goods/images'){
        $image = $request->file('file');
        $domain = config('aliyun.oss.domain');
        Storage::makeDirectory('public/'.$path);
        $file_name = Str::random(6).'.'.$image->getClientOriginalExtension();
        $image->storeAs('public/'.$path,$file_name);
        $img =  'storage/'.$path.'/'.$file_name;
        $this->ossUpload($path.'/'.$file_name,$img);
        $ossPath = $domain.$path.'/'.$file_name;
        if($request->filled('wxUpload')){
            $wxupload = (new ApplymentsController())->uploadImage($img);
            if($request->filled('bizlicense')){
                $json = $this->bizlicense($ossPath);
                return [$domain.$path.'/'.$file_name,$wxupload,$json];
            }
            if($request->filled('idcard')){
                $json = $this->idcard($ossPath);
                return [$domain.$path.'/'.$file_name,$wxupload,$json];
            }
            return [$domain.$path.'/'.$file_name,$wxupload];
        }
        Storage::delete('public/'.$path.'/'.$file_name);
        return $ossPath;
    }

    public function ossUpload($object,$filePath)
    {
        //storage/appgoods/images/PuiFVT.png
        $config = config('aliyun.defaults');
        $oss = config('aliyun.oss');
        try{
            $ossClient = new OssClient($config['accessKeyId'],$config['accessKeySecret'],$oss['endpoint']);
            $ossClient->uploadFile($oss['bucket'], $object, $filePath);
        }catch(OssException $e){
            info($e);
            return $e;
        }
        return $object;
    }
    /**
     * @param $object 为图片相对路径 前缀不加 ‘/’
     * @return mixed
     *
     */
    public function ossDelImg($object){
        $config = config('aliyun.defaults');
        $oss = config('aliyun.oss');
        try{
            $ossClient = new OssClient($config['accessKeyId'],$config['accessKeySecret'],$oss['endpoint']);
            $ossClient->deleteObject($oss['bucket'], $object);;
        }catch(OssException $e){
        }
        return $object;
    }
    //建立两个目录 temple 和product
//如果目标Bucket已开启版本控制，OSS将会为新拷贝的Object自动生成唯一的版本ID，
//此版本ID将会在响应Header的x-oss-version-id中返回。如果目标Bucket未曾开启或者暂停了版本控制，
//OSS将会为新拷贝的Object自动生成version ID为”null“的版本，且会覆盖原versionId为”null“的版本。
    public function copyImg($object){
        $config = config('aliyun.web');
        $versionId = null;
        try{
            $ossClient = new OssClient($config['accessKeyId'],$config['accessKeySecret'],$config['endpoint']);
            $ret = $ossClient->copyObject($config['bucket'], 'temple/'.$object, $config['bucket'], 'product/'.$object, array(OssClient::OSS_VERSION_ID => $versionId));
            // 删除指定versionId的Object。
            $ossClient->deleteObject($config['bucket'], 'temple/'.$object, array(OssClient::OSS_VERSION_ID => $versionId));
            return $ret;
        }catch(OssException $e){
            //info($e);
            info('阿里云拷贝失败'.$e->getMessage());
        }
    }
    //判断文件是否存在
    public function isSetImg($object){
        $config = config('aliyun.web');
        try{
            $ossClient = new OssClient($config['accessKeyId'],$config['accessKeySecret'],$config['endpoint']);
            $exist = $ossClient->doesObjectExist($config['bucket'], $object);
            return $exist;
        }catch(OssException $e){
            info('阿里云文件获取失败'.$e->getMessage());
        }
    }
    //验证资源是否存在
    public  function doesObjectExist($bucket,$fileName)
    {
        $config = $this->config();
        $ossClient = $this->ossClient($config);
        return $ossClient->doesObjectExist($bucket,$fileName);
    }
    public function ocrImage(){

    }
}
